package com.ljc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljc.entity.Post;
import com.ljc.search.model.PostDocment;
import com.ljc.search.mq.PostMqIndexMessage;
import com.ljc.search.repository.PostRepository;
import com.ljc.service.PostService;
import com.ljc.service.SearchService;
import com.ljc.vo.PostVo;

import lombok.extern.slf4j.Slf4j;

import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static javax.swing.text.html.FormSubmitEvent.MethodType.POST;

/**
 * @Author: ljc
 * @Date:Created in 19:26 2022/2/23
 */
@Slf4j
@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    PostRepository postRepository;
    @Autowired
    ModelMapper modelmapper;
    @Autowired
    PostService  postservice;

    @Override
    public IPage search(Page page, String keyword) {
        //分页信息 mybatisplus 的page 专程   jap的page
        Long current = page.getCurrent() - 1;
        Long size = page.getSize()+3;
        Pageable pageable = PageRequest.of(current.intValue(), size.intValue());
        //搜索es得到page
        MultiMatchQueryBuilder multiMatchQueryBuilder =
                QueryBuilders.multiMatchQuery(keyword, "title", "authorName", "categoryName");

        org.springframework.data.domain.Page<PostDocment> docments =
                postRepository.search(multiMatchQueryBuilder, pageable);
        //jpa的page 转成   mybatisplus的   page
        IPage pageData = new Page(page.getCurrent(), page.getSize(), docments.getTotalElements());
        pageData.setRecords(docments.getContent());
        return pageData;
    }
   //保存到ES

    @Override
    public int initESData(List<PostVo> records) {
        if (records==null||records.isEmpty()) {
        return 0;
        }
        List <PostDocment> docments=new ArrayList<>();
        for (PostVo vo : records) {
            //映射转换
            PostDocment postDocment = modelmapper.map(vo, PostDocment.class);
            docments.add(postDocment);
        }
        //可能出现问题
        postRepository.deleteAll();
        postRepository.saveAll(docments);

        return docments.size();
    }

    @Override
    public void createOrUpdateIndex(PostMqIndexMessage message) {
        Long postId = message.getPostId();
        PostVo postVo = postservice.selectOnePost(new QueryWrapper<Post>()
                .eq("p.id", postId));

        PostDocment postDocment = modelmapper.map(postVo, PostDocment.class);
        postRepository.save(postDocment);
        log.info("ES索引更新成功:"+postDocment.toString());
    }

    @Override
    public void remove(PostMqIndexMessage message) {
        postRepository.deleteById(message.getPostId());
        log.info("ES索引删除成功:"+message.toString());
    }
}
